package com.demo.controller;

import com.demo.util.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/uploadFile")
@CrossOrigin(origins = "http://localhost:8080")
public class DemoController {

    private static final boolean UUID_NAME = false;  // 是否用uuid命名

    @Autowired
    private FtpUtil ftpUtil;

    /**
     * 上传文件一
     */
    @PostMapping("/upload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String, String> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "上传文件失败");
        String fileName = file.getOriginalFilename();//获取文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//获取文件的后缀名
        fileName = getDate("yyyyMMddhhmmss") + UUID.randomUUID().toString().replaceAll("-", "") + "." + suffixName;
        //上传的文件名也需要加上后缀，不然虚拟机不知道文件格式
        InputStream inputStream = file.getInputStream();
        String filePath = null;
        Boolean flag = ftpUtil.uploadFile(fileName, inputStream);
        if (flag == true) {
            //上传文件成功
            filePath = ftpUtil.ftp_basepath + fileName;
            map.put("code", "1");
            map.put("msg", "上传文件成功");
        }
        map.put("path", filePath);
        System.out.println(map);
        return map;
    }

    /**
     * 上传文件二
     */
    @PostMapping("/uploadTwo")
    @ResponseBody
    public Map<String, String> uploadTwo(@RequestParam MultipartFile file) {
        Map<String, String> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "上传文件失败");
        String filePath;  // 文件路径
        // 文件原始名称
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);  // 获取文件后缀
        File outFile;
        if (UUID_NAME) {  // uuid命名
            filePath = getDate("") + UUID.randomUUID().toString().replaceAll("-", "") + "." + suffixName;
            outFile = new File(ftpUtil.ftp_basepath + filePath);
        } else {  // 使用原名称，存在相同着加(1)
            String prefix = fileName.substring(0, fileName.lastIndexOf("."));  // 获取文件名称
            filePath = getDate("") + fileName;
            outFile = new File( ftpUtil.ftp_basepath + filePath);
            int sameSize = 1;
            while (outFile.exists()) {
                filePath = getDate("") + prefix + "(" + sameSize + ")." + suffixName;
                outFile = new File( ftpUtil.ftp_basepath + filePath);
                sameSize++;
            }
        }
        try {
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            file.transferTo(outFile);
        } catch (Exception e) {

            e.printStackTrace();
            return map;
        }
        map.put("code", "1");
        map.put("msg", "上传文件成功");
        map.put("path", filePath);
        return map;
    }

    private String getDate(String format) {
        if (format == null || "".equals(format)) {
            format = "yyyy/MM/dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }

}
